package com.bytemonster.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
@Slf4j
public class PoolConfig {

    @Bean("networkExecutor")
    public Executor poolFactoryFactory() {
        return new ThreadPoolExecutor(
                4,
                8,
                60L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(100),
                r -> {
                    Thread t = new Thread(r, "poll-network-thread");
                    t.setUncaughtExceptionHandler((thread, ex) -> log.error("线程 {} 异常", thread.getName(), ex));
                    return t;
                },
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
}
